package com.mtt.thirdapi.yanxuan.controller;
import com.alibaba.fastjson.JSONObject;
import com.mtt.thirdapi.common.api.ResultCode;
import com.mtt.thirdapi.yanxuan.service.YXCallbackService;
import com.mtt.thirdapi.yanxuan.service.YXOpenApiService;
import com.mtt.thirdapi.common.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("yanxuan")
@Slf4j
public class YXCallbackController {
    @Autowired
    YXOpenApiService yxOpenApiService;
    @Autowired
    YXCallbackService yxCallbackService;

    /**
     * 严选回调
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("callback")
    public R callBack(HttpServletRequest request, HttpServletResponse response, @RequestBody String params) {
        log.info("appKey={}", request.getParameter("appKey"));
        log.info("method={}", request.getParameter("method"));
        log.info("timestamp={}", request.getParameter("timestamp"));
        String methodName = request.getParameter("method");
        switch (methodName) {
            case "yanxuan.callback.order.cancel":
                String orderCancelResult = request.getParameter("orderCancelResult");
                yxCallbackService.orderCancel(orderCancelResult);
                break;
            case "yanxuan.notification.order.exceptional":
                String exceptionInfo = request.getParameter("exceptionInfo");
                yxCallbackService.orderExceptional(exceptionInfo);
                break;
            case "yanxuan.notification.order.delivered":
                String orderPackage = request.getParameter("orderPackage");
                yxCallbackService.orderDelivered(orderPackage);
                break;
            case "yanxuan.notification.order.refund.address":
                String refundAddress = request.getParameter("refundAddress");
                yxCallbackService.refundAddress(refundAddress);
               break;
            case "yanxuan.notification.order.refund.reject":
                String rejectInfo = request.getParameter("rejectInfo");
                yxCallbackService.refundReject(rejectInfo);
                break;
            case "yanxuan.notification.order.refund.express.confirm":
                //严选在收到相关的退货包裹之后，将此包裹信息通知给渠道。 默认不通知渠道，需注册
                String expressConfirm = request.getParameter("expressConfirm");
                yxCallbackService.refundExpressConfirm(expressConfirm);
                break;
            case "yanxuan.notification.order.refund.system.cancel":
                //严选发起退货地址回调之后，如果超过14天没有收到渠道方绑定售后寄回物流单号，会取消相关的退货申请。
                // 建议渠道关注此回调，并同步更新退货申请单状态。 默认不通知渠道，需注册
                String systemCancel = request.getParameter("systemCancel");
                yxCallbackService.refundSystemCancel(systemCancel);
                break;
            case "yanxuan.notification.order.refund.result":
                //退款结果回调
                //严选会将最终的可退款结果通知渠道，会出现部分sku退款或者同一sku多个数量但只退其中一部分的情况。
                // 建议渠道关注此回调，并同步给用户。 默认不通知渠道，需注册
                String refundResult = request.getParameter("refundResult");
                yxCallbackService.refundResult(refundResult);
                break;
            case "yanxuan.notification.inventory.count.check":
                String skuCheck = request.getParameter("skuCheck");
                yxCallbackService.inventoryCountCheck(skuCheck);
                break;
        }
        return R.success(ResultCode.SUCCESS);
    }


    /**
     * 获取渠道已注册的回调方法名   获取渠道已注册的回调方法名，严选只会回调注册了的方法名
     *
     * @return
     */
    @GetMapping("/getCallBackMethods")
    public R getCallback() {
        return yxOpenApiService.handleGetCallBackMeheods();
    }

    /**
     * 自助注册回调
     *
     * @param methods 需注册的回调方法名，多个方法名用英文逗号分隔，覆盖原来所有方法
     * @return
     */
    @PostMapping("/registerCallback")
    public R registerCallback(String methods) {
        return yxOpenApiService.handleRegisterCallback(methods);
    }

    /**
     * 測試環境中使用：，模擬 触发异常订单回调 通过该请求触发订单异常回调（yanxuan.notification.order.exceptional）
     *
     * @param orderId
     * @return
     */

    @PostMapping("mockCallbackOrderCancel")
    public R mockCallbackOrderCancel(String orderId) {
        return yxOpenApiService.handleMockCallbackOrderCancel(orderId);
    }

    /**
     * 测试环境使用： 模拟  触发审核取消订单回调 通过该请求触发订单取消回调（yanxuan.callback.order.cancel）
     * 只能使用一次，第二次则会报 order status must be CANCELLING, current order status is KF_CANCEL
     *
     * @param orderId
     * @return
     */
    @PostMapping("/mockCallbackAuditCancelOrder")
    public R callbackAuditCancelOrder(String orderId, boolean cancel) {
        return yxOpenApiService.handleMockCallbackAuditCancelOrder(orderId, cancel);
    }

    /**
     * 测试环境使用：模拟 触发包裹物流办单回调  之后可以确认订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/mockCallbackDeliveryOrder")
    public R CallbackDeliveryOrder(String orderId) {
        return yxOpenApiService.handleMockCallbackDeliveryOrder(orderId);
    }

    /**
     * 测试环境使用：触发退货地址回调
     * 申请单类型：  1：无理由(普通退货) 2：质量问题(顺丰到付
     *
     * @param applyId
     * @param type
     * @return
     */
    @PostMapping("/mockCallbackRefundAddress")
    public R mockCallbackRefundAddress(String applyId, int type) {
        return yxOpenApiService.handleMockCallbackRefundAddress(applyId, type);
    }


    /**
     * 测试环境使用：触发严选拒绝退货回调
     *
     * @param applyId
     * @return
     */
    @PostMapping("/mockCallbackRefundReject")
    public R mockCallbackRefundReject(@NotBlank String applyId) {
        return yxOpenApiService.handleMockCallbackRefundReject(applyId);
    }

    /** 触发严选退货包裹确认收货回调
     * @param applyId
     * @return
     */
    @PostMapping("/mockCallbackRefundExpressConfirm")
    public R mockCallbackRefundExpressConfirm(String applyId) {
        return yxOpenApiService.handleMockCallbackRefundExpressConfirm(applyId);
    }

    /**
     *  触发严选系统取消退货回调
     * @param applyId
     * @return
     */
    @PostMapping("/mockCallbackRefundSystemCancel")
    public R mockCallbackRefundSystemCancel(@NotBlank String applyId){
        return yxOpenApiService.handleMockCallbackRefundSystemCancel(applyId);
    }

    /**
     * 触发严选直接退款结果回调
     * @param applyId
     * @return
     */
    @PostMapping("/mockCallbackRefundResultDirectly")
    public R mockCallbackRefundResultDirectly(@NotBlank String applyId){
        return yxOpenApiService.handleCallbackRefundResultDirectly(applyId);
    }

    /**
     * 触发退货包裹确认收货后退款结果回调
     * 测试不用触发，联系严选人员触发
     * @param applyId 申请单Id
     * @param allApproved 是否该申请单下所有sku都审核同意退款，默认为true
     * @return
     */
    @PostMapping("/mockCallbackRefundResultWithExpress")
    public R mockCallbackRefundResultWithExpress(@NotBlank String applyId,boolean allApproved){
        return yxOpenApiService.handleMockCallbackRefundResultWithExpress(applyId,allApproved);
    }
}
